﻿// Main JS File

// Base JS classes extensisons
String.prototype.startsWith = function(pString) {
var vMatch = this.search(new RegExp("^" + pString));
	return (vMatch != -1);
}

// YUI utility objects
var Event;
var Dom;

// Load YUI components: 
(function() {
	var gLoader = new YAHOO.util.YUILoader({
		base: "http://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/",
		require: ["connection", "event", "layout", "dom", "element"],
		combine: true,
		onSuccess: function() {
			Event = YAHOO.util.Event;
			Dom = YAHOO.util.Dom;
			
			Event.onDOMReady(function() {
				gSiteLayoutCtrl.init();
				gSiteLayoutCtrl.layout.render();
			});
		}
	});

	gLoader.insert();
})();

gUtils = {};
gUtils.findAllButtons = function() {
	var vButts = Dom.getElementsBy(function(pEl) {
		return (pEl.tagName == 'BUTTON' || (pEl.tagName == 'INPUT' && (pEl.type == 'submit' || pEl.type == 'button')));
	});
	return vButts;
}

// Site Layout
var gSiteLayoutCtrl = {
	layout: null,

	init: function() {
	this.layout = new YAHOO.widget.Layout(document.body, {
			units: [{ position: 'top', height: 65, body: 'top'},
					{ position: 'center', body: 'middle', scroll: true},
					{ position: 'bottom', body: 'bottom', height: 150}] 
		});
	}
};

// Ajax Controller
var gAjaxer = {
	postSignature: null,

	defautCallback: {
		success: function(o) {
			var vData;
			var vCType = o.getResponseHeader["Content-Type"];

			if (vCType.startsWith("application/json")) {
				vData = eval("(" + o.responseText + ")");
			}
			else if (vCType.startsWith("application/xml")) {
				vData = o.responseXML;
			}
			else {
				vData = o.responseText;
			}

			return vData;
		},

		failure: function(o) {
			alert("default" + o.statusText);
		}
	},

	startRequest: function(pUri, pMethod, pCallback, pPost) {
		if (!pCallback) {
			pCallback = this.defautCallback;
		}
		if (!pPost)
			pPost = null;
		if (!pMethod)
			pMethod = 'GET';
		else if (pMethod == 'POST')
			pPost += '&__signature=' + encodeURIComponent(this.postSignature);

		YAHOO.util.Connect.asyncRequest(pMethod, pUri, pCallback, pPost);
	}
};

// Form Submission Controller
var gFormsCtrl = {
	disbaleButtons: function() {
		var vButts = gUtils.findAllButtons();
		if (vButts) {
			this.lastDisabledButtons = new Array();
			for (vI in vButts) {
				if (!vButts[vI].disabled) {
					vButts[vI].disabled = true;
					new YAHOO.util.Element(vButts[vI]).addClass('disabled');
					this.lastDisabledButtons.push(vButts[vI]);
				}
			}
		}
	},

	enableButtons: function() {
		if (this.lastDisabledButtons) {
			for (vI in this.lastDisabledButtons) {
				this.lastDisabledButtons[vI].disabled = false;
				new YAHOO.util.Element(this.lastDisabledButtons[vI]).removeClass('disabled');
			}
			this.lastDisabledButtons = new Array();
		}
	},

	callback: {
		arguments: [],

		success: function(o) {
			var vData = gAjaxer.defautCallback.success(o);

			if (vData.errors) {
				var vForm = this.arguments[0];
				var vFormErrs = Dom.get(vForm.id + 'Errors');
				if (vFormErrs) {
					var yFormErrs = new YAHOO.util.Element(vFormErrs);
					vFormErrs.innerHTML = vData.errors;
					if (vFormErrs.innerHTML != "") yFormErrs.removeClass("hidden");
					else yFormErrs.addClass("hidden");
				}
			}

			if (vData.reload) {
				if (window.location.reload) window.location.reload(true);
				else window.history.go(0);
				return;
			}
			else if (vData.redirect) {
				window.location.href = vData.redirect;
				return;
			}

			gFormsCtrl.enableButtons();
		},

		failure: function(o) {
			alert(o.statusText);
			gFormsCtrl.enableButtons();
		}
	},

	submit: function(pEv, pForm) {
		if (window.attachEvent) pEv.returnValue = false;
		else pEv.preventDefault();

		YAHOO.util.Connect.setForm(pForm);
		this.callback.arguments[0] = pForm;
		this.disbaleButtons();
		gAjaxer.startRequest(pForm.action, 'POST', this.callback);

		return false;
	}
};
